package net.maku.system.erp.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.system.erp.convert.SysClientsConvert;
import net.maku.system.erp.dao.SysClientsDao;
import net.maku.system.erp.entity.SysClientsEntity;
import net.maku.system.erp.query.SysClientsQuery;
import net.maku.system.erp.service.SysClientsService;
import net.maku.system.erp.vo.SysClientsVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 客户实体
 *
 * @author 王宝林 baolin.wang@aliyun.com
 * @since 1.0.0 2024-03-22
 */
@Service
@AllArgsConstructor
public class SysClientsServiceImpl extends BaseServiceImpl<SysClientsDao, SysClientsEntity> implements SysClientsService {

    @Override
    public PageResult<SysClientsVO> page(SysClientsQuery query) {
        IPage<SysClientsEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(SysClientsConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<SysClientsEntity> getWrapper(SysClientsQuery query){
        LambdaQueryWrapper<SysClientsEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StrUtil.isNotBlank(query.getClientCode()), SysClientsEntity::getClientCode, query.getClientCode());
        wrapper.like(StrUtil.isNotBlank(query.getClientName()), SysClientsEntity::getClientName, query.getClientName());
        wrapper.like(StrUtil.isNotBlank(query.getDutyParagraph()), SysClientsEntity::getDutyParagraph, query.getDutyParagraph());

        return wrapper;
    }

    @Override
    public void save(SysClientsVO vo) {
        SysClientsEntity entity = SysClientsConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(SysClientsVO vo) {
        SysClientsEntity entity = SysClientsConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}